import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:edu/constants/app_strings.dart';
import 'package:edu/utils/account_info.dart';
import 'package:edu/utils/app_router.dart';
import 'package:edu/utils/color_extension.dart';

class AccountCancellationPage extends StatefulWidget {
  const AccountCancellationPage({super.key});

  @override
  State<AccountCancellationPage> createState() =>
      _AccountCancellationPageState();
}

class _AccountCancellationPageState extends State<AccountCancellationPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text(AppStrings.accountCancellation)),
      body: _buildMainBody(context),
    );
  }

  Widget _buildMainBody(BuildContext context) {
    final account =
        AccountInfo.shared.userProfile?.phoneNumber ??
        AccountInfo.shared.userProfile?.email;
    return SafeArea(
      child: Column(
        children: [
          const SizedBox(height: 10),
          const Row(
            children: [
              Spacer(),
              Icon(Icons.error, size: 70, color: Colors.grey),
              Spacer(),
            ],
          ),
          if (account != null) const SizedBox(height: 10),
          if (account != null)
            Text(
              '${AppStrings.will} $account ${AppStrings.cancelTailTip}',
              style: Theme.of(
                context,
              ).textTheme.titleLarge?.copyWith(fontWeight: FontWeight.bold),
            ),
          const SizedBox(height: 10),
          Text(
            AppStrings.cancelHintHeader,
            style: Theme.of(
              context,
            ).textTheme.titleSmall?.copyWith(color: Colors.grey),
          ),
          const SizedBox(height: 20),
          _buildCancelHintTexts(context),
          const SizedBox(height: 10),
          _buildCancelButtonTip(context),
          const SizedBox(height: 20),
          _buildMainButtonRow(context),
        ],
      ),
    );
  }

  Widget _buildMainButtonRow(BuildContext context) {
    return Container(
      height: 44,
      width: double.infinity,
      padding: const EdgeInsets.symmetric(horizontal: 20),
      child: ElevatedButton(
        onPressed: _mainButtonPressed,
        style: ButtonStyle(
          shape: WidgetStateProperty.all<RoundedRectangleBorder>(
            RoundedRectangleBorder(borderRadius: BorderRadius.circular(6.0)),
          ),
        ),
        child: Text(
          AppStrings.agreeCancel,
          style: Theme.of(context).textTheme.titleMedium,
        ),
      ),
    );
  }

  void _mainButtonPressed() {}

  Widget _buildCancelButtonTip(BuildContext context) {
    final textSpans = <TextSpan>[
      TextSpan(
        text: AppStrings.cancelButtonTip,
        style: Theme.of(context).textTheme.titleSmall,
      ),
      TextSpan(
        text: AppStrings.cancelPrivacyPolicy,
        style: Theme.of(context).textTheme.titleSmall?.copyWith(
          color: Theme.of(context).colorScheme.primary,
        ),
        recognizer: TapGestureRecognizer()
          ..onTap = () {
            final background = Theme.of(context).scaffoldBackgroundColor;
            final bg = background.toHex();
            final fg =
                Theme.of(context).textTheme.titleMedium?.color?.toHex() ??
                background.inverse.toHex();
            context.pushNamed(
              AppRouter.kWebViewPage,
              queryParameters: {
                'page': 'usercancellation',
                'title': AppStrings.userCancelPolicy,
                'bg': bg,
                'fg': fg,
              },
            );
          },
      ),
    ];

    return RichText(
      text: TextSpan(children: textSpans),
      maxLines: 2,
      overflow: TextOverflow.ellipsis,
      textAlign: TextAlign.center,
    );
  }

  Widget _buildCancelHintTexts(BuildContext context) {
    return Container(
      width: double.infinity,
      color: Theme.of(context).colorScheme.surface,
      child: Column(
        children: [
          Container(
            width: double.infinity,
            padding: const EdgeInsets.symmetric(horizontal: 30),
            color: Theme.of(context).colorScheme.surface,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const SizedBox(height: 20),
                Text(
                  AppStrings.cancelHint1,
                  style: Theme.of(context).textTheme.titleMedium,
                ),
                const SizedBox(height: 10),
                Text(
                  AppStrings.cancelHint2,
                  style: Theme.of(context).textTheme.titleMedium,
                ),
                const SizedBox(height: 10),
                Text(
                  AppStrings.cancelHint3,
                  style: Theme.of(context).textTheme.titleMedium,
                ),
                const SizedBox(height: 10),
                Text(
                  AppStrings.cancelHint4,
                  style: Theme.of(context).textTheme.titleMedium,
                ),
                const SizedBox(height: 30),
              ],
            ),
          ),
          const Divider(height: 1, color: Colors.grey),
          const SizedBox(height: 20),
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 30),
            child: Text(
              AppStrings.cancelHintFooter,
              style: Theme.of(context).textTheme.bodySmall,
            ),
          ),
          const SizedBox(height: 20),
        ],
      ),
    );
  }
}
